package cn.neusoft.controller;


import cn.neusoft.domain.*;
import cn.neusoft.service.CargoAndDescriptionService;
import cn.neusoft.service.DepBillService;
import cn.neusoft.service.WarehouseAdministrateService;
import cn.neusoft.tool.PageSet;
import cn.neusoft.vo.ResultBean;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseAdministrateService warehouseAdministrateService;
    @Autowired
    private DepBillService depBillService;
    @Autowired
    private CargoAndDescriptionService cargoAndDescriptionService;
    private static final String UNSETTED_UNIT = "未设置";
    private static final String UNSETTED_ID = "不存在";


    @PostMapping("/queryWarehouseStorage")
    public ResultBean queryWarehouseStorage(Map<String, Object> map){
        ResultBean resultBean = null;

        if(map != null){
            try{
                int warehouseID = (int) map.get("warehouseID");
                String desName = (String) map.get("desName");

                Warehouse warehouse = warehouseAdministrateService.getWarehouseByID(warehouseID);
                WarehouseCarSituation warehouseCarSituation = null;

                double totalQuantities = 0;
                double preparedQuantities = 0;
                double distributedQuantities = 0;

                totalQuantities = warehouse.getCargos().get(desName);

                List<CargoDeployBill> list = depBillService.searchDepBill(warehouseID);
                for(CargoDeployBill c : list){
                    if(c.getStage() != DeployBillStageEnum.PRE_OUT){
                        continue;
                    }
                    for(Map.Entry<String, Double> entry : c.getPlanCargosInf().entrySet()){
                        if(entry.getKey().equals(desName)){
                            preparedQuantities += entry.getValue();
                        }
                    }
                }
                distributedQuantities = totalQuantities - preparedQuantities;
                warehouseCarSituation = new WarehouseCarSituation(warehouse.getId(), warehouse.getName(), totalQuantities, preparedQuantities, distributedQuantities);
                resultBean = ResultBean.success(warehouseCarSituation);
            }catch(Exception e){
                resultBean =  ResultBean.error(7063, "异常情况");

            }
            resultBean =  ResultBean.error(7062, "数据为空");


        }
        else{
            resultBean =  ResultBean.error(7060, "数据为空");

        }


        return resultBean;
    }


    private class WarehouseCarSituation{
        private int warehouseID;
        private String warehouseName;
        private double totalQuantities;
        private double preparedQuantities;
        private double distributedQuantities;

        public WarehouseCarSituation(int warehouseID, String warehouseName, double totalQuantities, double preparedQuantities, double distributedQuantities) {
            this.warehouseID = warehouseID;
            this.warehouseName = warehouseName;
            this.totalQuantities = totalQuantities;
            this.preparedQuantities = preparedQuantities;
            this.distributedQuantities = distributedQuantities;
        }

        public int getWarehouseID() {
            return warehouseID;
        }

        public void setWarehouseID(int warehouseID) {
            this.warehouseID = warehouseID;
        }

        public String getWarehouseName() {
            return warehouseName;
        }

        public void setWarehouseName(String warehouseName) {
            this.warehouseName = warehouseName;
        }

        public double getTotalQuantities() {
            return totalQuantities;
        }

        public void setTotalQuantities(double totalQuantities) {
            this.totalQuantities = totalQuantities;
        }

        public double getPreparedQuantities() {
            return preparedQuantities;
        }

        public void setPreparedQuantities(double preparedQuantities) {
            this.preparedQuantities = preparedQuantities;
        }

        public double getDistributedQuantities() {
            return distributedQuantities;
        }

        public void setDistributedQuantities(double distributedQuantities) {
            this.distributedQuantities = distributedQuantities;
        }



    }



}
